package com.pubinfo.passbook.data.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.common.service.cache.JedisManage;
import com.pubinfo.passbook.common.mapper.TSubsidiaryPrivateKeyMapper;
import com.pubinfo.passbook.common.model.bo.SubsidiaryInfo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "redis")
@Api(tags = "redis管理")
@Slf4j
public class RedisController {
  
  @Autowired
  TSubsidiaryPrivateKeyMapper keyMapper;
  @Autowired
  JedisManage redisService;
  
  @GetMapping("subsidiary-list")
  @ApiOperation(value = "查询公司列表")
  public JsonResult<List<String>> subsidiaryList() {
    return JsonResult.success(redisService.getList("subsidiary-info"));
  }
  
  @GetMapping("object")
  @ApiOperation(value = "指定key查询String")
  public JsonResult<String> object(String key) {
    return JsonResult.success(redisService.getStr(key));
  }
  
  @GetMapping("getList")
  @ApiOperation(value = "指定key查询list")
  public JsonResult<List<String>> getList(String key) {
    return JsonResult.success(redisService.getList(key));
  }
  
  @GetMapping("num")
  @ApiOperation(value = "查询连接数信息")
  public JsonResult<String> num() {
    return JsonResult.success(redisService.numInfo());
  }
  
  @GetMapping("close")
  @ApiOperation(value = "关闭连接池")
  public JsonResult<String> close() {
    redisService.close();
    return JsonResult.success("成功");
  }
  
  @GetMapping("organization")
  @ApiOperation(value = "查询组织树")
  public JsonResult<JSONObject> organization() {
    String organization = redisService.getStr("queues_dzpj");
    if (StringUtils.isNotBlank(organization)) {
      JSONObject jsonObject = JSONObject.parseObject(organization);
      return JsonResult.success(jsonObject);
    }
  
    return JsonResult.success(null);
  }
  
  @GetMapping("save")
  @ApiOperation(value = "保存人员数据")
  public JsonResult<String> save(@RequestBody String json) {
    return JsonResult.success(redisService.putStr("queues_dzpj", json));
  }
  
  @DeleteMapping("delete")
  @ApiOperation(value = "删除指定键的值")
  public JsonResult<Long> delete(String key) {
    return JsonResult.success(redisService.deleteByStrKey(key));
  }
  
  @SneakyThrows
  @PostMapping("saveFile")
  @ApiOperation(value = "保存人员数据-文件")
  public JsonResult<String> saveFile(@RequestParam("uploadFile") MultipartFile file) {
    InputStream inputStream = file.getInputStream();
    String str = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    return JsonResult.success(redisService.putStr("queues_dzpj", str));
  }
  
  @GetMapping("subsidiary-sync")
  @ApiOperation(value = "同步数据库公司数据到redis")
  public JsonResult<Long> toRedis() {
    List<SubsidiaryInfo> infoList = keyMapper.listTaxAllSubsidiaryInfo(null);
    List<String> objectList = new ArrayList<>();
    infoList.forEach(subsidiaryInfo -> {
      String infoStr = JSON.toJSONString(subsidiaryInfo);
      objectList.add(infoStr);
    });
    String[] strings = objectList.toArray(new String[objectList.size()]);
    
    return JsonResult.success(redisService.putList("subsidiary-info", strings));
  }
  
}
